package com.mzm.ui.admin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Currency;
import java.util.List;

import javax.swing.DefaultComboBoxModel;

import org.apache.commons.lang.StringUtils;

import com.mzm.api.RecommendationDto;

/**
 * 
 * @author User
 */
public class RecommendationEditorPanel extends javax.swing.JPanel {

	private static List<Currency> availableCurrencies = new ArrayList<Currency>(
			Currency.getAvailableCurrencies());

	static {
		orderCurrencies();
	}

	private RecommendationDto recommendationDto;

	/**
	 * Creates new form RecommendationEditorPanel
	 */
	public RecommendationEditorPanel() {
		initComponents();
		String[] currencies = new String[availableCurrencies.size()];
		for (int i = 0; i < availableCurrencies.size(); i++) {
			Currency c = availableCurrencies.get(i);
			currencies[i] = c.getDisplayName() + " [" + c.getCurrencyCode() + "]";
		}
		currencyCombo.setModel(new DefaultComboBoxModel(currencies));
	}

	/**
	 * Gets the <code>RecommendationDto</code> represented by this editor.
	 * 
	 * @return
	 */
	public RecommendationDto getRecommendationDto() {
		RecommendationDto uiData = getUiDataAsRecommendationDto();
		return uiData;
	}

	public boolean dataWasModified() {
		RecommendationDto uiData = getUiDataAsRecommendationDto();
		if (uiData.equals(recommendationDto)) {
			return false;
		}
		return true;
	}

	private RecommendationDto getUiDataAsRecommendationDto() {
		RecommendationDto r = new RecommendationDto();
		r.setContent(contentTA.getText());
		r.setCurrencyCode(getSelectedCurrency());
		r.setGrade(Integer.parseInt(gradeTF.getText()));
		r.setGradeRange(Integer.parseInt(gradeRangeTF.getText()));
		r.setKeywords(Arrays.asList(StringUtils.split(keywordsTA.getText(), ",")));
		r.setMediaInfo(null);
		r.setPrice(Integer.parseInt(priceTF.getText()));
		r.setRecommenderName(recommenderNameTF.getText());
		if (recommendationDto != null) {
			r.setId(recommendationDto.getId());
		}
		return r;
	}

	public void setRecommendationDto(RecommendationDto r) {
		this.recommendationDto = r;
		contentTA.setText(r.getContent());
		int currencyIndex = getCurrencyIndex(r.getCurrencyCode());
		if (currencyIndex >= 0) {
			currencyCombo.setSelectedIndex(currencyIndex);
		}
		gradeTF.setText(Integer.toString(r.getGrade()));
		gradeRangeTF.setText(Integer.toString(r.getGradeRange()));
		StringBuilder sb = new StringBuilder();
		List<String> keywords = r.getKeywords();
		for (String k : keywords) {
			sb.append(k);
			sb.append(", ");
		}
		keywordsTA.setText(sb.toString());
		priceTF.setText(Integer.toString(r.getPrice()));
		recommenderNameTF.setText(r.getRecommenderName());
	}

	public String getSelectedCurrency() {
		String s = (String) currencyCombo.getSelectedItem();
		String[] splits = StringUtils.split(s, "[]");
		return splits[splits.length - 1];
	}

	public void setSelectedCurrency(String currencyCode) {
		int currencyIndex = getCurrencyIndex(currencyCode);
		currencyCombo.setSelectedIndex(currencyIndex);
	}

	private int getCurrencyIndex(String currencyCode) {
		for (int i = 0; i < availableCurrencies.size(); i++) {
			Currency c = availableCurrencies.get(i);
			if (c.getCurrencyCode().equals(currencyCode)) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jScrollPane4 = new javax.swing.JScrollPane();
		remarksTA1 = new javax.swing.JTextArea();
		jLabel3 = new javax.swing.JLabel();
		currencyCombo = new javax.swing.JComboBox();
		jLabel4 = new javax.swing.JLabel();
		sheetNumTF = new javax.swing.JTextField();
		jLabel5 = new javax.swing.JLabel();
		gradeTF = new javax.swing.JTextField();
		gradeRangeTF = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		jLabel7 = new javax.swing.JLabel();
		jLabel8 = new javax.swing.JLabel();
		jScrollPane2 = new javax.swing.JScrollPane();
		remarksTA = new javax.swing.JTextArea();
		jScrollPane3 = new javax.swing.JScrollPane();
		contentTA = new javax.swing.JTextArea();
		jLabel9 = new javax.swing.JLabel();
		priceTF = new javax.swing.JTextField();
		jLabel10 = new javax.swing.JLabel();
		mediaSelectionCombo1 = new javax.swing.JComboBox();
		jLabel11 = new javax.swing.JLabel();
		jScrollPane5 = new javax.swing.JScrollPane();
		keywordsTA = new javax.swing.JTextArea();
		jLabel12 = new javax.swing.JLabel();
		recommenderNameTF = new javax.swing.JTextField();

		remarksTA1.setColumns(20);
		remarksTA1.setRows(5);
		jScrollPane4.setViewportView(remarksTA1);

		jLabel3.setText("מדיה:");

		jLabel4.setText("מספר גליון:");

		jLabel5.setText("ציון:");

		jLabel6.setText("מתוך:");

		jLabel7.setText("תוכן ההמלצה:");

		jLabel8.setText("הערות:");

		remarksTA.setColumns(20);
		remarksTA.setRows(5);
		jScrollPane2.setViewportView(remarksTA);

		contentTA.setColumns(20);
		contentTA.setRows(5);
		jScrollPane3.setViewportView(contentTA);

		jLabel9.setText("מחיר:");

		jLabel10.setText("מטבע:");

		jLabel11.setText("מילות מפתח:");

		keywordsTA.setColumns(20);
		keywordsTA.setRows(5);
		jScrollPane5.setViewportView(keywordsTA);

		jLabel12.setText("שם ממליץ:");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(79, 79, 79)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		413,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jLabel9,
																						javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						jLabel11,
																						javax.swing.GroupLayout.Alignment.TRAILING)))
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
																.addGroup(
																		layout.createSequentialGroup()
																				.addGap(0,
																						0,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel3))
																.addGroup(
																		layout.createSequentialGroup()
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																								.addGroup(
																										layout.createSequentialGroup()
																												.addComponent(
																														currencyCombo,
																														0,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														Short.MAX_VALUE)
																												.addGap(18,
																														18,
																														18)
																												.addComponent(
																														jLabel10)
																												.addGap(168,
																														168,
																														168))
																								.addGroup(
																										layout.createSequentialGroup()
																												.addGroup(
																														layout.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING)
																																.addGroup(
																																		layout.createSequentialGroup()
																																				.addGap(0,
																																						0,
																																						Short.MAX_VALUE)
																																				.addComponent(
																																						priceTF,
																																						javax.swing.GroupLayout.PREFERRED_SIZE,
																																						71,
																																						javax.swing.GroupLayout.PREFERRED_SIZE))
																																.addComponent(
																																		jScrollPane2)
																																.addComponent(
																																		jScrollPane5))
																												.addGap(61,
																														61,
																														61)))
																				.addComponent(
																						jLabel8))
																.addGroup(
																		javax.swing.GroupLayout.Alignment.TRAILING,
																		layout.createSequentialGroup()
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING)
																								.addGroup(
																										layout.createSequentialGroup()
																												.addComponent(
																														gradeRangeTF,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														71,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addPreferredGap(
																														javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																												.addComponent(
																														jLabel6)
																												.addPreferredGap(
																														javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																												.addComponent(
																														gradeTF,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														79,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addPreferredGap(
																														javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																												.addComponent(
																														jLabel5)
																												.addPreferredGap(
																														javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														Short.MAX_VALUE)
																												.addComponent(
																														recommenderNameTF,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														116,
																														javax.swing.GroupLayout.PREFERRED_SIZE))
																								.addComponent(
																										jScrollPane3)
																								.addGroup(
																										javax.swing.GroupLayout.Alignment.LEADING,
																										layout.createSequentialGroup()
																												.addComponent(
																														sheetNumTF,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														71,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addGap(27,
																														27,
																														27)
																												.addComponent(
																														jLabel4)
																												.addPreferredGap(
																														javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																														42,
																														Short.MAX_VALUE)
																												.addComponent(
																														mediaSelectionCombo1,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														174,
																														javax.swing.GroupLayout.PREFERRED_SIZE)))
																				.addGap(26, 26, 26)
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																								.addComponent(
																										jLabel7,
																										javax.swing.GroupLayout.Alignment.TRAILING)
																								.addComponent(
																										jLabel12,
																										javax.swing.GroupLayout.Alignment.TRAILING)))))
								.addGap(22, 22, 22)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(65, 65, 65)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel3)
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(
																		mediaSelectionCombo1,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(jLabel4)
																.addComponent(
																		sheetNumTF,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(jLabel5)
																.addComponent(
																		gradeRangeTF,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(jLabel6)
																.addComponent(
																		gradeTF,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		recommenderNameTF,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addComponent(jLabel12))
								.addGap(28, 28, 28)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jScrollPane3,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel7))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel8)
												.addComponent(jScrollPane2,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel9)
												.addComponent(priceTF,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel10)
												.addComponent(currencyCombo,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel11)
												.addComponent(jScrollPane5,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(104, 104, 104)));
	}// </editor-fold>//GEN-END:initComponents
		// Variables declaration - do not modify//GEN-BEGIN:variables

	private javax.swing.JTextArea contentTA;
	private javax.swing.JComboBox currencyCombo;
	private javax.swing.JTextField gradeRangeTF;
	private javax.swing.JTextField gradeTF;
	private javax.swing.JLabel jLabel10;
	private javax.swing.JLabel jLabel11;
	private javax.swing.JLabel jLabel12;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JScrollPane jScrollPane4;
	private javax.swing.JScrollPane jScrollPane5;
	private javax.swing.JTextArea keywordsTA;
	private javax.swing.JComboBox mediaSelectionCombo1;
	private javax.swing.JTextField priceTF;
	private javax.swing.JTextField recommenderNameTF;
	private javax.swing.JTextArea remarksTA;
	private javax.swing.JTextArea remarksTA1;
	private javax.swing.JTextField sheetNumTF;

	// End of variables declaration//GEN-END:variables

	private static void orderCurrencies() {
		Collections.sort(availableCurrencies, new Comparator<Currency>() {

			@Override
			public int compare(Currency c1, Currency c2) {
				if (c1.getCurrencyCode().equals("ILS")) {
					return -1000;
				}
				if (c2.getCurrencyCode().equals("ILS")) {
					return 1000;
				}
				if (c1.getCurrencyCode().equals("USD")) {
					return -900;
				}
				if (c2.getCurrencyCode().equals("USD")) {
					return 900;
				}
				if (c1.getCurrencyCode().equals("EUR")) {
					return -800;
				}
				if (c2.getCurrencyCode().equals("EUR")) {
					return 800;
				}
				return 0;
			}
		});
	}
}
